package com.sumapay.fsb.gensrc.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sumapay.fsb.gensrc.entity.FsbBankCard;
import com.sumapay.fsb.gensrc.mapper.FsbBankCardMapper;
import com.sumapay.fsb.gensrc.service.IFsbBankCardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author suma
 * @since 2021-03-16
 */
@Service
public class FsbBankCardServiceImpl extends ServiceImpl<FsbBankCardMapper, FsbBankCard> implements IFsbBankCardService {
    @Override
    public String getUserCard(Map<String, Object> map) {
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("ACTION_RESULT",1);

        QueryWrapper<FsbBankCard> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PHONE_NO", map.get("PHONE_NO").toString());
        String ss = queryWrapper.getExpression().getSqlSegment();
        System.out.println(ss);
        List<FsbBankCard> list = null;
        try{
            list = baseMapper.selectList(queryWrapper);
            for(FsbBankCard fbc :list){
                Map<String,String> cardBin = baseMapper.getCardBin(fbc.getBankAccount());
                fbc.setFld1(String.valueOf(cardBin.get("CARD_TYPE")));
                fbc.setFld2(cardBin.get("BANK_NAME"));
            }
        }catch (Exception e){
            returnMap.put("MESSAGE","查询银行卡出错，请稍后重试");
            return JSON.toJSONString(returnMap);
        }
        returnMap.put("RESULT_LIST",list);
        returnMap.put("COUNT",list.size());
        returnMap.put("ACTION_RESULT",0);

        return JSON.toJSONString(returnMap);

    }

    @Override
    public String bindUserCard(Map<String, Object> map) {
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("ACTION_RESULT",1);

        QueryWrapper<FsbBankCard> queryWrapper = new QueryWrapper();
        queryWrapper.eq("BANK_ACCOUNT",map.get("BANK_ACCOUNT").toString());
        queryWrapper.eq("BANK_ACCOUNT_NAME",map.get("BANK_ACCOUNT_NAME").toString());
        queryWrapper.eq("PHONE_NO",map.get("PHONE_NO").toString());
        queryWrapper.eq("ID_NUMBER",map.get("ID_NUMBER").toString());
        if(baseMapper.selectOne(queryWrapper)!=null){
            returnMap.put("MESSAGE","MAC该卡号已绑定过，请勿重复绑卡");
            return JSON.toJSONString(returnMap);
        }
        FsbBankCard fsbBankCard = new FsbBankCard();
        fsbBankCard.setBankAccount(map.get("BANK_ACCOUNT").toString());
        fsbBankCard.setBankAccountName(map.get("BANK_ACCOUNT_NAME").toString());
        fsbBankCard.setPhoneNo(map.get("PHONE_NO").toString());
        fsbBankCard.setIdNumber(map.get("ID_NUMBER").toString());
        if(baseMapper.insert(fsbBankCard)>0){
            returnMap.put("ACTION_RESULT",0);
        }else{
            returnMap.put("message","服务器错误,请联系客服或稍后重试!");
        }


        return JSON.toJSONString(returnMap);
    }

    @Override
    public String getCardBin(String cardNo) {
        return JSON.toJSONString(this.baseMapper.getCardBin(cardNo));
    }
}
